from flask import Blueprint,request,make_response,jsonify,redirect
import requests
import random
from application.models.users.user import *
# from application.models.users.user import db
from db import db
import time
from utils.Myjwt import myjwt
ke_blue = Blueprint("kecheng",__name__)
# 类别展示
@ke_blue.route('/lei')
def kecheng():
    res = Ctype.query.filter_by().all()
    data = []
    for i in res:
        data.append({
            'id': i.id,
            'name': i.name,
        })
    print(data)
    return jsonify({'code':200,'data':data})
# 标签表的展示
@ke_blue.route('/biao')
def biao():
    res = Tags.query.filter_by().all()
    data = []
    for i in res:
        data.append({
            'id': i.id,
            'name': i.name,
        })
    print(data)
    return jsonify({'code':200,'data':data})
# 分类的展示
@ke_blue.route('/fenlei')
def kejian():
    res = CourseType.query.all()
    data = []
    for i in res:
        data.append({
            'id': i.id,
            'name': i.name,
            'descrip':i.descrip,

        })
    print(data)
    return jsonify({'code':200,'data':data})
@ke_blue.route('/ke')
def ke():
    cid = request.args.get('cid')
    res = Course.query.filter_by(ctype=cid).all()
    data = []
    for i in res:
        data.append({
            'id': i.id,
            'name': i.name,
            'descrip':i.descrip,

        })
    print(data)
    return jsonify({'code':200,'data':data})

def getTags():
    sql = "select * from tags"
    res = db.findAll(sql)
    return res

def getctype():
    sql = "select * from ctype"
    res = db.findAll(sql)
    return res


@ke_blue.route("/home")
def gethome():
    ctypetlist = getctype()
    tagslist = getTags()
    return jsonify({
        "code": 200,
        "ctypelist": ctypetlist,
        "tagslist": tagslist
    })
@ke_blue.route("/getHomeCourse")
def getHomeGourse1():
    # 获取类型
    ctypetlist = getctype()
    # 获取标签
    tagslist = getTags()
    # 获取当前页面
    page = request.args.get("page")
    # 获取查询条件
    tagid = request.args.get("tagid")
    typeid = request.args.get("type")
    is_publish = request.args.get("is_publish")
    orderbyid = request.args.get("orderbyid")
    where = ""

    if int(typeid) > 0:
        where = where + " and c.type=" + typeid
    if int(is_publish) > 0:
        where = where + " and c.is_publish=" + is_publish
    if int(tagid) > 0:
        tsql = "select count(c.id) as tcount from course as c inner join course_tag as cl on cl.courseid=c.id  where cl.tagid="+tagid
        sql = "select * from course as c inner join course_tag as cl on cl.courseid=c.id  where cl.tagid="+tagid
    else:
        sql = "select * from course as c  where 1=1"
        tsql = "select count(c.id) as  tcount  from course as c  where 1=1"
    if where:
        sql = sql + " " + where
        tsql = tsql + " " + where
    if int(orderbyid) > 0:
        if int(orderbyid) == 1:
            # 等于1倒叙
            sql = sql + " order by c.id desc"
        else:
            # 等于2按点击数
            sql = sql + " order by c.views desc"
    # 查询总共有多少条
    res = db.findone(tsql)
    total = res['tcount']
    page_size = 2
    start = (int(page)-1)*page_size
    sql = sql + " limit %d,%d" % (start, page_size)
    courselist = db.findAll(sql)
    print(sql)
    return jsonify({"code": 200, "tlist": ctypetlist, 'taglist': tagslist, 'clist': courselist, "total": total, "page_size":  page_size})
@ke_blue.route('/jie')
def jie():
    jid = request.args.get('jid')
    v = Jie.query.filter_by(courseid=jid).all()
    print(v)
    list = []
    for i in v:
        list.append({
            'id':i.id,
            'name':i.name,
            'url':i.url,
            'tcount':i.tcount,
        })
    return jsonify({'code':200,'data':list})
@ke_blue.route('/Shou')
def shou():
    uid = request.args.get('userid')
    cid = request.args.get('courseid')
